package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.VideoEquipment;
import com.hgxd.result.Result;

import com.hgxd.service.IVideoEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *摄像头设备 前端控制器
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "摄像头设备接口")
@RestController
@RequestMapping("/api/video-equipment")
@CrossOrigin
public class VideoEquipmentController {
    @Resource(name = "VideoEquipmentServiceImpl")
    private IVideoEquipmentService videoEquipmentService;
    /**
     * 根据组织名字来获取最新的设备信息
     */
    @PostMapping("/getNewDataByOrganizationName/{organizationName}")
    @ApiOperation(value = "根据组织名字来获取最新的设备信息", notes = "根据组织名字来获取最新的设备信息")
    @ApiImplicitParam(name = "organizationName", value = "组织名称")
    @OperationLogAnnotation(operModule = "摄像头设备-根据组织名字来获取最新的设备信息",operType = "获取",operContent = "根据组织名字来获取最新的设备信息")
    public Result<List<VideoEquipment>> getNewDataByOrganizationName(@PathVariable String organizationName) {
        List<VideoEquipment> videoEquipments= videoEquipmentService.getNewDataByOrganizationName(organizationName);
        return Result.ok(videoEquipments);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增摄像头", notes = "新增摄像头")
    @ApiImplicitParam(name = "videoEquipment", value = "摄像头")
    @OperationLogAnnotation(operModule = "摄像头设备-新增",operType = "新增",operContent = "新增摄像头设备")
    public Result<String> insert(@RequestBody VideoEquipment videoEquipment) {
        boolean save = videoEquipmentService.save(videoEquipment);
        return save ? Result.ok("新增成功") : Result.fail("新增失败");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改摄像头信息", notes = "修改摄像头信息")
    @ApiImplicitParam(name = "videoEquipment", value = "摄像头")
    @OperationLogAnnotation(operModule = "摄像头设备-更新",operType = "更新",operContent = "更新摄像头设备")
    public Result<String> update(@RequestBody VideoEquipment videoEquipment) {
        boolean data = videoEquipmentService.updateById(videoEquipment);
        return data ? Result.ok("修改成功") : Result.fail("修改失败");
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除摄像头", notes = "根据id删除摄像头")
    @ApiImplicitParam(name = "id", value = "id")
    @OperationLogAnnotation(operModule = "摄像头设备-删除",operType = "删除",operContent = "删除摄像头设备")
    public Result<String> delete(@PathVariable Integer id) {
        boolean data = videoEquipmentService.removeById(id);
        return data ? Result.ok("删除成功") : Result.fail("删除失败");
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除摄像头", notes = "批量删除摄像头")
    @ApiImplicitParam(name = "ids", value = "id的集合")
    @OperationLogAnnotation(operModule = "摄像头设备-批量删除",operType = "删除",operContent = "批量删除摄像头设备")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        boolean data = videoEquipmentService.removeByIds(ids);
        return data ? Result.ok("批量删除成功") : Result.fail("批量删除失败");
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部摄像头", notes = "查询全部摄像头")
    @OperationLogAnnotation(operModule = "摄像头设备-查询全部",operType = "查询全部",operContent = "查询全部摄像头设备")
    public Result<List<VideoEquipment>> findAll() {
        return Result.ok(videoEquipmentService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个摄像头", notes = "根据id查询一个摄像头")
    @ApiImplicitParam(name = "id", value = "id")
    @OperationLogAnnotation(operModule = "摄像头设备-根据id查询一个摄像头",operType = "查询",operContent = "根据id查询一个摄像头")
    public Result<VideoEquipment> findOne(@PathVariable Integer id) {
        VideoEquipment byId = videoEquipmentService.getById(id);
        if (byId==null){
            return Result.fail();
        }else {
            return Result.ok(byId);
        }
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    @ApiOperation(value = "分页模糊查询摄像头", notes = "分页模糊查询摄像头")
    @OperationLogAnnotation(operModule = "摄像头设备-分页模糊查询",operType = "分页模糊查询",operContent = "分页模糊查询摄像头设备")
    public Result<Page<VideoEquipment>> findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<VideoEquipment> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(videoEquipmentService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

